package com.check.operation.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.ViewById;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.CheckApplication;
import com.check.operation.R;
import com.check.operation.model.MessagRe;
import com.check.operation.model.SsroomPa;
import com.check.operation.model.Sysdiclist;
import com.check.operation.model.UserRe;
import com.szj.android.util.UpdateVersion;
import com.szj.basic.view.BaseActivity;
import com.szj.util.HttpUtil;
import com.szj.util.JSONUtil;
import com.szj.util.SysUtil;

import android.content.Intent;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;


@EActivity(R.layout.activity_login)
@Fullscreen
public class LoginActivity extends BaseActivity {
	
	final String TAG="LOGIN";
	@App
	CheckApplication checkApp;
	
	@ViewById
	EditText yhm;
	
	@ViewById
	EditText mm;
	
	@AfterViews
	public void initView(){
		//更新
//		new UpdateVersion(this,"version/apk.json","http://199.199.4.20:8085/apps/").execute();
		checkApp.addActivity(this);
		String[] lxs={"1001","1002","1003","1004","1005","1006","1010","1011","1020","1021","1022","1023","1024"};
		String action="sysdic/getsysdiclist/";
		try{
			
			for(String lx:lxs){
				String rs=HttpUtil.postJSON(action+lx, "");
				MessagRe<List<Sysdiclist>> ms=JSONObject.parseObject(rs, new TypeReference<MessagRe<List<Sysdiclist>>>(){});
				List<Sysdiclist> dics= ms.value;
				Map<String,String> ma=new HashMap<String, String>();
				checkApp.getDics().put(lx, ma);
				for(Sysdiclist li:dics){
					ma.put(li.dic_code, li.dic_name);
				}
				if("1021".equals(lx)){
					checkApp.setSsbwall(dics);
				}
				if("1024".equals(lx)){
					checkApp.setSfdgdic(dics);
				}
			}
		}catch(Exception e){
			Log.e(TAG,e.getMessage());
		}
		
		action="sys/servicetime";
		String rs=HttpUtil.get(action);
		
		try {
			SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
			Date date=simpleDateFormat.parse(rs);
			checkApp.setDate(date);
		} catch (ParseException e) {
			checkApp.setDate(new Date());
		}
		
	}

	@Click
	public void dl(){
		String username=yhm.getText().toString();
		String password=mm.getText().toString();
		String action="user/login";
	
		Map<String,Object> params=checkApp.getParams();
		params.put("loginname", username);
		params.put("pwd", password);
		String json=JSONObject.toJSONString(params);
		String rs=HttpUtil.postJSON(action, json);
		if(SysUtil.isNotNUll(rs)){
			UserRe user=(UserRe) JSONUtil.fromJson(rs, UserRe.class);
			if("success".equals(user.getCode())){
				checkApp.setUserRe(user);
				//获得手术房间
				params.clear();
				params.put("user", user.getUser());
				params.put("request", "{}");
				action="sysdic/getrooms";
				rs=HttpUtil.postJSON(action, JSONObject.toJSONString(params));
				SsroomPa ssroomPa=(SsroomPa) JSONUtil.fromJson(rs, SsroomPa.class);
				checkApp.setSsrooms(ssroomPa.value);
				Toast.makeText(this, "欢迎登录系统："+user.getUser().EmpName, Toast.LENGTH_SHORT).show();
				
				Intent intent=new Intent(this,MainActivity_.class);
				startActivity(intent);
			}else{
				Toast.makeText(this,user.getText(),Toast.LENGTH_SHORT).show();
			}
		}
		
	}
	
	

}
